﻿using Book.Entities;
using Book.IRepositories;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Book.Controllers
{
    [Route("api/menu")]
    [ApiController]
    [EnableCors("any")]
    public class MenuController : ControllerBase
    {
        private readonly IMenuRepository _menuRepository;

        public MenuController(IMenuRepository menuRepository)
        {
            this._menuRepository = menuRepository;
        }
       [HttpGet("getmenujson")]
       public IActionResult GetMenuJson()
        {
            var rel = _menuRepository.GetMenuJson();
            return Ok(rel);
        }
        [HttpGet("gettreejson")]
        public IActionResult GetTreeJson()
        {
            var rel = _menuRepository.GetTreeJson();
            return Ok(rel);
        }
        [HttpGet("getmenubypid")]
        public IActionResult GetMenuByPId(string id)
        {
            var rel = _menuRepository.GetMenuByPId(id);
          
            return Ok(rel);
        }
        [HttpPost("getonemenubyid")]
        public IActionResult GetOneMenuById([FromBody] JObject parameter)
        {
            string id = parameter.Value<string>("id");
            var rel = _menuRepository.GetOneMenuById(id);
            return Ok(rel);
        }
        [HttpPost("deletemenu")]
        public IActionResult DeleteMenu([FromBody] JObject parameter)
        {
            string id = parameter.Value<string>("id");
            bool res=_menuRepository.DeleteMenu(id);
            return Ok(res);
        }
        [HttpPost("addmenu1")]
        public IActionResult AddMenu1(MenuEntity menu)
        {
            _menuRepository.AddMenu1(menu);
            return Ok();          
        }
        [HttpPost("addmenu")]
        public IActionResult AddMenu([FromBody] JObject parameter)
        {
            string name = parameter.Value<string>("name");
            string ename = parameter.Value<string>("ename");
            int template = parameter.Value<int>("template");
            string parentid = parameter.Value<string>("parentid");
            _menuRepository.AddMenu(name, ename,template, parentid);
            return Ok();
        }
        [HttpPost("updatemenu")]
        public IActionResult UpdateMenu([FromBody] JObject parameter)
        {

            string name = parameter.Value<string>("name");
            string ename = parameter.Value<string>("ename");
            string url = parameter.Value<string>("url");
            int template = parameter.Value<int>("template");
            string id = parameter.Value<string>("id");
            _menuRepository.UpdateMenu(name, ename, template, id);
            return Ok();
        }
        [HttpPost("updateorder")]
        public IActionResult UpdateOrder([FromBody] JObject parameter)
        {
            string id = parameter.Value<string>("id");
            string pid = parameter.Value<string>("parentid");
            double order = parameter.Value<double>("order");
            _menuRepository.UpdateOrder(id,pid,order);
            return Ok();
        }
        }
}
